package com.car.shopping.controller;


import cn.hutool.core.bean.BeanUtil;
import com.car.commons.core.model.Result;
import com.car.commons.core.util.JwtUtil;
import com.car.commons.web.util.WebUtil;
import com.car.shopping.controller.form.CantCommodityShouhouForm;
import com.car.shopping.controller.form.ShoppingSHForm;
import com.car.shopping.entity.CommodityShouhou;
import com.car.shopping.service.CommodityShouhouService;
import com.car.shopping.service.dto.CantCommodityShouhouDto;
import com.car.shopping.service.param.CantCommodityShouhouParam;
import com.car.shopping.service.param.ShoppingSHParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@RestController
@RequestMapping("/commodityShouhou")
@Api(tags = "商品售后相关接口")
public class CommodityShouhouController {
    @Autowired
    private CommodityShouhouService commodityShouhouService;


    //生成商品退货售后（订单号，描述，图片，退货方式）
    @ApiOperation("提交商品退货或换货申请售后")
    @PostMapping("/shoppingSH")
    public Result shoppingSH(
            @RequestParam("commodityNum") String commodityNum, // 订单号
            @RequestParam("commodityCause") String commodityCause, // 退货原因
            @RequestParam("commodityReturnid") Integer commodityReturnid, // 1换货2退货
            @RequestParam(value = "commodityImgs", required = false) MultipartFile commodityImgs,
            @RequestParam("commodityTracking") String commodityTracking // 运单号
    ) throws Exception {
        // 1. 从请求头中获取 token
        String token = WebUtil.getRequest().getHeader("car-token");

        // 2. 从 token 中解析出 userId
        Integer userId = JwtUtil.getUserId(token);

        // 3. 创建 ShoppingSHParam 对象
        ShoppingSHParam param = new ShoppingSHParam();
        param.setCommodityNum(commodityNum);
        param.setCommodityCause(commodityCause);
        param.setCommodityImgs(commodityImgs);
        param.setCommodityTracking(commodityTracking);
        param.setCommodityUserid(userId);
        param.setCommodityReturnid(commodityReturnid);

        // 4. 调用服务层逻辑，提交退货或换货申请
        commodityShouhouService.shoppingSH(param);

        // 5. 返回成功结果
        return Result.getResult().setMsg("提交商品退换货售后申请成功").setCode(200);
    }


    //通过退货审核（商品售后id）生成新订单 要记日志
    @ApiOperation("通过换货审核&生成新订单")
    @PostMapping("/allRight.pt")
    public Result allRight(@RequestBody Map<String, Integer> requestBody) throws Exception {
        Integer commodityId = requestBody.get("commodityId");
        commodityShouhouService.allRight(commodityId);
        return Result.getResult().setMsg("商品换货售后申请已通过，请检查订单").setCode(200);
    }


    //售后驳回（商品售后id，驳回原因）
    @ApiOperation("售后驳回")
    @PostMapping("/cantCommodityShouhou.pt")
    public Result cantCommodityShouhou(@RequestBody CantCommodityShouhouForm form)throws Exception{
        commodityShouhouService.cantCommodityShouhou(BeanUtil.toBean(form, CantCommodityShouhouParam.class));
        return Result.getResult().setMsg("驳回成功").setCode(200);
    }
    //查询售后申请
    @ApiOperation("根据id查询售后申请")
    @PostMapping("/selectShById.ty")
    public Result<CantCommodityShouhouDto> selectShById(Integer commodityId)throws Exception{
        CantCommodityShouhouDto dto =  commodityShouhouService.selectShById(commodityId);
        return Result.getResult(CantCommodityShouhouDto.class).setMsg("根据id查成功").setCode(200).setData(dto);
    }


    @ApiOperation("根据用户id查询售后申请")
    @PostMapping("/selectShByUserId")
    public Result<List<CommodityShouhou>> selectShByUserId() throws Exception {
        // 1. 从请求头中获取 token
        String token = WebUtil.getRequest().getHeader("car-token");

        // 2. 从 token 中解析出 userId
        Integer commodityUserid = JwtUtil.getUserId(token);

        // 3. 查询该用户的售后申请
        List<CommodityShouhou> sh = commodityShouhouService.selectShByUserId(commodityUserid);

        // 4. 返回成功结果
        return Result.getListResult(CommodityShouhou.class).setMsg("根据id查成功").setCode(200).setData(sh);
    }





    @ApiOperation("查询所有售后申请")
    @GetMapping("/selectAllSh.pt")
    public Result<List<CommodityShouhou>> selectAllSh()throws Exception{
        List<CommodityShouhou> sh =  commodityShouhouService.selectAllSh();
        return Result.getListResult(CommodityShouhou.class).setMsg("查询所有售后申请成功").setCode(200).setData(sh);
    }

    // 查询所有换货的售后申请 (CommodityReturnid = 1)
    @ApiOperation("查询所有换货的售后申请")
    @GetMapping("/selectHH.pt")
    public Result<List<CommodityShouhou>> selectHH() throws Exception {
        List<CommodityShouhou> sh = commodityShouhouService.selectHH();
        return Result.getListResult(CommodityShouhou.class)
                .setMsg("查询所有换货的售后申请成功")
                .setCode(200)
                .setData(sh);
    }

    // 查询所有退货的售后申请 (CommodityReturnid = 2)
    @ApiOperation("查询所有退货的售后申请")
    @GetMapping("/selectTH.pt")
    public Result<List<CommodityShouhou>> selectTH() throws Exception {
        List<CommodityShouhou> sh = commodityShouhouService.selectTH();
        return Result.getListResult(CommodityShouhou.class)
                .setMsg("查询所有退货的售后申请成功")
                .setCode(200)
                .setData(sh);
    }




}

